Imports IICModel.BusObjects.Mappers
Imports IICModel.ConversionObjects.Mappers
Imports IICModel.BusObjects
Imports IICModel.ConversionObjects

Namespace DataConbversionLogic
    Public Class PASCandSubClassMember
        Inherits BaseDataConverter

        Private CAND_PRT_db As CAND_PRTDBMapper = New CAND_PRTDBMapper(newDBConnection)

        Private studentdb As New StudentDBMapper(newDBConnection)
        Private periodclassdb As New PeriodClassDBMapper(newDBConnection)
        Private lessondb As New LessonDBMapper(newDBConnection)

        Protected Overrides Sub mapData(ByVal source As org.model.lib.Model.IModelObject, _
                                        ByRef destination As org.model.lib.Model.IModelObject)

            Dim s As CAND_SUB = CType(source, CAND_SUB)
            Dim d As ClassMember = CType(destination, ClassMember)
            'If s.MemberNo = 4810 Then Stop

            If String.IsNullOrEmpty(s.ExamPeriod) Then
                Me.logMsg(s.Id & " skipped, ExamPeriod is empty ")
                destination = Nothing
                Exit Sub
            End If

            If s.MemberNo Is Nothing Then
                Me.logMsg(s.Id & " skipped, MemberNo is empty ")
                destination = Nothing
                Exit Sub
            End If

            'we must find a class from the period code
            Dim period As Period = PeriodDataUtils.findOne("diplomasupertypeid in (2,5) and PeriodCode={0}", s.ExamPeriod)
            If period Is Nothing Then
                Me.logMsg(s.Id & " skipped, Period not found from code " & s.ExamPeriod)
                destination = Nothing
                Exit Sub
            End If

            Dim st As Student = studentdb.findWhere("MemberNumber={0}", s.MemberNo)
            If st Is Nothing Then
                Me.logMsg(s.Id & " skipped, Student not found from MemberNumber " & s.MemberNo)
                destination = Nothing
                Exit Sub
            End If

            Dim periodClass As PeriodClass
            Dim classid As Integer
            Dim lesson As Lesson = LessonsCache.current.getLessonByCode(Trim(s.SUBJECT))
            'lessondb.findWhere("lessoncode={0}", s.SUBJECT)

            periodClass = periodclassdb.findWhere( _
                " lessonid = {0} " & _
                " and periodid={1}", lesson.LessonId, period.PeriodId)

            If periodClass Is Nothing Then
                Me.logMsg(s.Id & " class not found, will create! ")
                periodClass = PeriodClassFactory.Create

                periodClass.PeriodId = period.PeriodId
                periodClass.LessonId = lesson.LessonId
                periodClass.PassGrade = INT_PAS_PASSING_GRADE
                periodClass.MaxGrade = 200
                periodClass.DataConversionSourceTable = getTableName()
                'periodClass.ExamDate =
                periodclassdb.save(periodClass)
                classid = periodClass.ClassId
            Else
                classid = periodClass.ClassId
            End If

            If Me.newdbMapper.findWhere("StudentId={0} and ClassId={1}", _
                                            st.Id, classid) IsNot Nothing Then
                'Me.logMsg(s.MemberNo & " skipped, wull result in Duplicate classmember ")
                destination = Nothing
                Exit Sub
            End If

            Dim lCAND_PRT As CAND_PRT = CAND_PRT_db.findWhere("[EXAM_PERIOD]={0} and [MEMBER_NO]={1}", s.ExamPeriod, s.MemberNo)
            If lCAND_PRT Is Nothing Then
                Me.logMsg(s.Id & " skipped, no CAND_PRT record found ")
                destination = Nothing
                Exit Sub
            End If
            Me.logMsg(s.MemberNo & " saving new record ")
            d.StudentId = st.Id

            d.CandidateCode = lCAND_PRT.CandNo
            d.ClassId = classid
            d.DataConversionSourceTable = getTableName()

            If s.MARKS = 0 Then
                d.Attend = False
                d.Grade = Nothing
            Else
                d.Grade = CType(s.MARKS, Integer?)
                d.Attend = True
            End If

            'the we set it so that we can save later
            d.PeriodClass.Period.IsOpen = True

        End Sub

        Protected Overrides Function getTableName() As String
            Return "CAND_SUB"
        End Function

        Protected Overrides Sub instantiateMappers()

            Me.dataConvMapper = New CAND_SUBDBMapper()
            Me.newdbMapper = New ClassMemberDBMapper

        End Sub

        Protected Overrides Function checkData(ByVal source As IModelObject, _
                                  ByVal destination As IModelObject) As Boolean

            Return True

        End Function


    End Class
End Namespace